package common;

import org.junit.Test;

public class TryWithResource {

	@Test
	public void test() throws Exception {
		Connection conn = null;
		Exception e = null;
		try {
			conn = new Connection();
			conn.sendData();
		} catch (Exception sendDataException) {
			e = sendDataException;
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception closeException) {
					if (e != null) {
						e.addSuppressed(closeException);
					} else {
						e = closeException;
					}
				}
			}
			if(e!=null) {
				throw e;
			}
		}
	}

	@Test
	public void test1() throws Exception {
		try(Connection conn = new Connection()) {
			conn.sendData();
		}
	}

	class Connection implements AutoCloseable {
		public void sendData() throws Exception {
			throw new Exception("send data");
		}

		@Override
		public void close() throws Exception {
			throw new Exception("close");
		}
	}

}
